package com.xiaocg.base;

import java.util.Collections;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * @author xiaocg
 */

class Share{
    private  int number =0;
    public synchronized void  incr() throws InterruptedException {
        if (number != 0 ){
            this.wait();
        }
        number++;
        System.out.println(Thread.currentThread().getName()+" :: "+number);

        this.notifyAll();
    }

    public synchronized void  decr() throws InterruptedException {
        if (number != 1){
            this.wait();
        }
        number--;
        System.out.println(Thread.currentThread().getName()+" :: "+number);
        this.notifyAll();
    }
}
/**
 * @author xiaocg
 */
public class ThreadDemo1 {
    public static void main(String[] args) {
        Share share = new Share();
        new Thread(() -> {
           for (int i=1;i<=10;i++){
               try {
                   share.incr();
               } catch (InterruptedException e) {
                   e.printStackTrace();
               }
           }
        },"aa").start();

        new Thread(() -> {
            for (int i=1;i<=10;i++){
                try {
                    share.decr();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"bb").start();



    }
}
